package com.baymax.springboot.cli.controller;

import com.baymax.springboot.cli.model.HdeUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiao.hu
 * @date 2021-08-29
 * @apiNote
 */
@Controller
@RequestMapping("/http-content-type")
public class HttpContentTypeController {

    @RequestMapping(value="/users",method= RequestMethod.POST,
            consumes = "application/json", produces = "application/json")
    @ResponseBody
    public List<HdeUser> users(@RequestBody HdeUser user) {
        List<HdeUser> usersList = new ArrayList<>();
        usersList.add(user);
        return usersList;
    }


    /**
     * 只对请求头为“Content-Type:application/x-www-form-urlencoded”的请求进行处理
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/request1", method = RequestMethod.POST,
            headers = "Content-Type=application/x-www-form-urlencoded")
    public String request1(HttpServletRequest request)  {
        //①得到请求的内容区数据的类型
        String contentType = request.getContentType();
        System.out.println("========ContentType:" + contentType);
        //②得到请求的内容区数据的编码方式，如果请求中没有指定则为null
        //注意，我们的CharacterEncodingFilter这个过滤器设置了编码(UTF-8)
        //编码只能被指定一次，即如果客户端设置了编码，则过滤器不会再设置
        String characterEncoding = request.getCharacterEncoding();
        System.out.println("========CharacterEncoding:" + characterEncoding);

        //③表示请求的内容区数据为form表单提交的参数，此时我们可以通过request.getParameter得到数据（key=value）
        System.out.println(request.getParameter("realname"));
        System.out.println(request.getParameter("username"));
        return "hde_user";
    }

    /**
     * 只对请求头为“Content-Type:application/json”的进行请求处理
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/request2", method = RequestMethod.POST,
            headers = "Content-Type=application/json")
    public String request2(HttpServletRequest request) throws IOException {
        //①表示请求的内容区数据为json数据
        InputStream is = request.getInputStream();
        byte[] bytes = new byte[request.getContentLength()];
        is.read(bytes);
        //②得到请求中的内容区数据（以CharacterEncoding解码）
        //此处得到数据后你可以通过如json-lib转换为其他对象
        String jsonStr = new String(bytes, request.getCharacterEncoding());
        System.out.println("json data：" + jsonStr);
        return "success";
    }


    /**
     * 返回html格式的内容
     * @param response
     * @throws IOException
     */
    @RequestMapping("/responseHtml")
    public void responseHtml(HttpServletResponse response) throws IOException {
        //①表示响应的内容区数据的媒体类型为html格式，且编码为utf-8(客户端应该以utf-8解码)
        response.setContentType("text/html;charset=utf-8");
        //②写出响应体内容
        response.getWriter().write("<font style='color:red'>hello</font>");
    }


    /**
     * 只对请求头为“Content-Type:application/xml”的进行请求处理
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/responseXml", headers = "Accept=application/xml")
    public void responseXml(HttpServletResponse response) throws IOException {
        //①表示响应的内容区数据的媒体类型为xml格式，且编码为utf-8(客户端应该以utf-8解码)
        response.setContentType("application/xml;charset=utf-8");
        //②写出响应体内容
        String xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        xmlData += "<user><username>zhang</username><password>123</password></user>";
        response.getWriter().write(xmlData);
    }
}
